
from OpenGL.GL import *
import numpy


def cross_product(vector1, vector2):
    '''
    This function defines a cross product between two vectors.
    '''
    vector = numpy.zeros(3)
    vector[0] = vector1[1]*vector2[2] - vector1[2]*vector2[1]
    vector[1] = vector1[2]*vector2[0] - vector1[0]*vector2[2]
    vector[2] = vector1[0]*vector2[1] - vector1[1]*vector2[0]
    
    return vector

def multiply_vector_by_matrix(vector):
    '''
    This function defines a product between a vector and a the current matrix. 
    '''
    aux_matrix = numpy.zeros(16)
    
    #Putting the vector's values in the first line of the matrix
    for i in range(len(vector)):
        aux_matrix[i] = vector[i]
    
    #Finding the matrix mode
    current_matrix_mode = glGetIntegerv(GL_MATRIX_MODE)
    
    glPushMatrix()
    glMultMatrixd(aux_matrix)
    if current_matrix_mode == GL_MODELVIEW:
        ret_matrix = glGetDouble(GL_MODELVIEW_MATRIX)
    elif current_matrix_mode == GL_PROJECTION:
        ret_matrix = glGetDouble(GL_PROJECTION_MATRIX)
    glPopMatrix()
    
    ret_matrix = ret_matrix.tolist()[0]
    
    if (ret_matrix[3] != 0):
        for i in ret_matrix:
            i /=  ret_matrix[3]
             
    return ret_matrix[:3]

def array_subtraction(a1, a2):
    return [a1[i] - a2[i] for i in range(len(a1))]

def normalized(vector):
    norm = numpy.linalg.norm(vector)
    return [ coord / norm for coord in vector ]
